<?php

class Application_Model_UserMapper
{
	protected $_dbTable;
 
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_User');
        }
        return $this->_dbTable;
    }
 
    public function save(Application_Model_User $User)
    {
        $data = array(
            'password'   => md5($User->getPassword()),
            'real_name'   => $User->getRealName(),
            'username' => $User->getUsername(),
            'email' => $User->getEmail(),
            'role' => $User->getRole(),
        );
 
        if (null === ($id = $User->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function delete(Application_Model_User $User)
    {
        $data = array(
            'password'   => $User->getPassword(),
            'real_name'   => $User->getRealName(),
            'username' => $User->getUsername(),
            'email' => $User->getEmail(),
            'role' => $User->getRole(),
            'id' => $User->getId(),
        );

        $id = $User->getId();

        $this->getDbTable()->delete(array('id = ?' => $id));
    }
 
    public function find($id, Application_Model_User $User)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $User->setId($row->id)
                  ->setUsername($row->username)
                  ->setPassword($row->password)
                  ->setRealName($row->real_name)
                  ->setEmail($row->email)
                  ->setRole($row->role);
    }

    public function findByUsername($username)
    {
        
        
        $query = 'username = "'.$username.'"';
        //$result = $this->getDbTable()->fetchRow($query);
        $select = $this->getDbTable()->select()->where('username = ?', $username);
        
        $row = $this->getDbTable()->fetchRow($select);
        if ($row == null) {
            return null;
        }
        
        $data = array("id" => $row->id, "username" => $row->username, "password" => $row->password, "real_name" => $row->real_name, "role" => $row->role);
        $User = new Application_Model_User($data);

        $User->setId($row->id)
                  ->setUsername($row->username)
                  ->setPassword($row->password)
                  ->setRealName($row->real_name)
                  ->setEmail($row->email)
                  ->setRole($row->role);

        return $User;
    }

 
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll(null, 'username asc');
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_User();
            $entry->setId($row->id)
                   ->setUsername($row->username)
                  ->setPassword($row->password)
                  ->setRealName($row->real_name)
                  ->setEmail($row->email)
                  ->setRole($row->role);
            $entries[] = $entry;
        }
        return $entries;
    }

}

